import React from 'react';
import { useCollapse } from '@/hooks';
import { Collapse } from '@/molecules';
import { Form } from 'antd';
import { BottomAction, AssessTask, DiscussArrange, AssessLeader } from '../containers';
import { useAssessSetting, useAssessTotal, useAssessLevel } from '../hooks';
import { TRIAL_ACTION, TALK } from '../constants';

const { Panel } = Collapse;

function AssessTutorW(props) {
    const { form, data = {} } = props;
    const { assessWeekly = [], interviewJoinCodes = [], assessAbility } = data;
    const { levels } = assessAbility || {};
    const assessTotal = useAssessTotal(data, form);
    const level = useAssessLevel(levels, assessTotal?.level);

    const detail = React.useMemo(
        () => assessWeekly.find(item => item.status === TRIAL_ACTION.ASSESS_TEACHER_WEEK) || {},
        [assessWeekly]
    );

    const { activeKey, onChange, openAll } = useCollapse(['1', '2', '3'], []);
    const { w = {} } = useAssessSetting(data);

    const showDiscuss = level === 1;
    const initInterviewType = TALK.WEEK;
    const params = React.useMemo(() => {
        const {
            resourceRelation = {},
            assessSummary = [],
            project,
            interviewWay,
            interviewLocation,
            interviewEmployeeCodeList,
            interviewDate,
            interviewStartTime,
            interviewEndTime,
            interviewType,
            schedule,
            ...rest
        } = form.getFieldsValue();
        const { uploadResourceFile } = resourceRelation;
        const interviewData = {
            interviewType: interviewType || initInterviewType,
            interviewWay,
            interviewLocation,
            interviewEmployeeCodeList,
            interviewStartTime: `${interviewDate?.format('YYYY-MM-DD')} ${interviewStartTime?.format('HH:mm:00')}`,
            interviewEndTime: `${interviewDate?.format('YYYY-MM-DD')} ${interviewEndTime?.format('HH:mm:00')}`,
            schedule
        };
        const interviewArrange = showDiscuss ? { interviewArrange: interviewData } : {};
        return {
            assess: {
                resourceRelation: uploadResourceFile ? [uploadResourceFile] : [],
                assessAbility: {
                    project
                },
                assessSummary: assessSummary[0],
                level: assessTotal.level, // 综合能力等级
                score: assessTotal.score, // 综合能力分数
                ...interviewArrange,
                ...rest
            }
        };
    }, [form, assessTotal]);

    return (
        <div>
            <Collapse bordered={false} activeKey={activeKey} onChange={onChange}>
                {w.showTask && (
                    <Panel header="任务情况" key="1">
                        <AssessTask data={data} />
                    </Panel>
                )}
                {w.showAssess && (
                    <Panel header="能力评估" key="2">
                        <AssessLeader form={form} data={data} detail={detail} assessTotal={assessTotal} />
                    </Panel>
                )}
                {w.showAssess && showDiscuss && (
                    <Panel header="新员工面谈安排" key="3">
                        <DiscussArrange
                            data={{ interviewEmployeeCodeList: interviewJoinCodes, interviewWay: 1 }}
                            form={form}
                            interviewType={initInterviewType}
                        />
                    </Panel>
                )}
            </Collapse>

            <BottomAction openAll={openAll} data={data} params={params} form={form} />
        </div>
    );
}

export default Form.create({})(AssessTutorW);
